package com.ciei.dpagm.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 
 * @author wangyan
 * @date:   2023/7/19
 */
public class WebUtils {

	/**
	 * 获取application/json 请求体json对象
	 * @param request
	 * @return
	 */
	public static JSONObject getRequestJsonObject(HttpServletRequest request){
		StringBuilder jsonBody = new StringBuilder();
		try {
			// 从请求中获取 BufferedReader 对象
			BufferedReader reader = request.getReader();
			// 逐行读取请求体内容
			String line;
			while ((line = reader.readLine()) != null) {
				jsonBody.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 解析JSON 请求体
		JSONObject requestJsonObject = JSON.parseObject(jsonBody.toString());
		return requestJsonObject;
	}

}
